////
//
/// \file ./test/internal_name_node_test.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/internal_name_node.hpp>

#include <string>

#include <gtest/gtest.h>

#include <inc/parser/utilities.hpp>

namespace inc {
namespace parser {

using std::string;

TEST(InternalNameNodeTest, parse_labels)
{
  const string data[] = { "foo", "foo_bar" };
  for( int i = 0 ; i < elementsof( data ) ; ++i )
  {
    InternalNameNode node;
    string::const_iterator last_parsed;
    EXPECT_TRUE( node.parse( data[i].begin(), data[i].end(), last_parsed ) )
        << "Failed to parse: '" << data[i] << "'";
    EXPECT_EQ( data[i], string( data[i].begin(), last_parsed ) );
    EXPECT_EQ( data[i], node.get_text() );
    EXPECT_TRUE( node.is_label() )
        << "Failed to parse label: '" << data[i] << "'";
  }
}

TEST(InternalNameNodeTest, parse_compound_names)
{
  const string data[] = { "( @foo )", "(@foo_bar)",
                          "(.foo)", "(.foo.bar)", "(@(.foo.bar))",
                          "(foo[0])", "(foo[x,y,z])", "(@(foo[0]))" };
  for( int i = 0 ; i < elementsof( data ) ; ++i )
  {
    InternalNameNode node;
    string::const_iterator last_parsed;
    EXPECT_TRUE( node.parse( data[i].begin(), data[i].end(), last_parsed ) )
        << "Failed to parse: '" << data[i] << "'";
    EXPECT_EQ( data[i], string( data[i].begin(), last_parsed ) );
    EXPECT_EQ( data[i], node.get_text() );
    EXPECT_TRUE( node.is_compound_name() )
        << "Failed to parse compound name: '" << data[i] << "'";
  }
}

#if 0
MY_TEST_NODE_SIMPLE( basic1, "foo" );
MY_TEST_NODE_SIMPLE( basic2, "foo.bar" );
MY_TEST_NODE_SIMPLE( basic3, "@foo" );
MY_TEST_NODE_SIMPLE( basic4, "foo[ bar ]" );

MY_TEST_NODE_SIMPLE( compound1, "( @foo ).bar" );
MY_TEST_NODE_SIMPLE( compound2, "( foo[0] ).bar" );
MY_TEST_NODE_SIMPLE( compound3, "( foo.bar ).bar" );
MY_TEST_NODE_SIMPLE( compound4, "(@(foo.bar)).baz" );
MY_TEST_NODE_SIMPLE( compound5, "@( foo.bar )" );
MY_TEST_NODE_SIMPLE( compound6, "@( foo.bar )" );
MY_TEST_NODE_SIMPLE( compound7, "@( foo.bar )" );
MY_TEST_NODE_SIMPLE( compound8, "@( foo[ 0 ] )" );
MY_TEST_NODE_SIMPLE( compound9, "( foo.bar )[ 0 ]" );
MY_TEST_NODE_SIMPLE( compound10, "( @foo )[ 0 ]" );
MY_TEST_NODE_SIMPLE( compound11, "@( (..foo.bar)[ 0 ] )" );
MY_TEST_NODE_SIMPLE( compound12, "@( (..foo.bar)[ 0 ] )" );
MY_TEST_NODE_SIMPLE( compound13, "( (@(..foo)).bar )[ 0 ]" );
MY_TEST_NODE_SIMPLE( compound14, "( @(..foo.bar) )[ 0 ]" );

MY_TEST_NODE_FAIL( broken1, "( ..foo.@bar )[ 0 ]" );
MY_TEST_NODE_FAIL( broken2, "( .foo.@(bar) )[ 0 ]" );
MY_TEST_NODE_FAIL( broken3, "@( foo.bar[0] )" );
MY_TEST_NODE_FAIL( broken4, "( foo.(bar)[0] )[ 0 ]" );
MY_TEST_NODE_FAIL( broken5, "( foo.bar[0].baz )[ 0 ]" );
#endif

} //  end parser namespace
} //  end inc namespace
